//
//  WetLandBookingViewController.m
//  homeland
//
//  Created by SunShine on 13-12-4.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "WetLandBookingViewController.h"
#import "BookingNoticeViewController.h"
#import "BookingFeeIntroViewController.h"
#import "BookingTicketCell.h"
#import "WetlandService.h"
#import "WetLandBookingOrderViewController.h"

@interface WetLandBookingViewController ()
@property (strong, nonatomic) NSString *infoStr;
@property (assign, nonatomic) CGFloat infoHeight;
@property (strong, nonatomic) NSArray *ticketsList;
@property (strong, nonatomic) NSMutableArray *checkedList;
@end

@implementation WetLandBookingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"门票";
    self.infoStr = @"      广东海珠国家湿地公园（简称“海珠湿地”）位于广州市海珠区东南部、广州新中轴线南段，总面积约869公顷，主要包括海珠湖和万亩果园核心区两部分，是全国特大城市最大、最美的城央国家湿地公园，被称为广州市的“绿心”和“南肺”。\n      海珠湿地积淀了珠江三角洲湿地千年的果基农业文化，独具繁华都市与自然风光相融、湿地生态系统结构特殊等特色，是候鸟迁徙重要通道、岭南水果发源地和岭南民俗文化荟萃区。海珠湿地目前已开放景区包括海珠湖、海珠湿地一期和海珠湿地二期，游人可充分领略“生态、岭南、水乡、野趣”的果林湿地特色风貌，是市民可及、可达、可享的都市休闲区。";
    if (RunTimeOSVersion >= 7.0) {
        self.infoHeight = [self caculateHeightForString:self.infoStr AndFontSize:16.0 AndWitdh:320.0];
    } else {
        self.infoHeight = [self caculateHeightForString:self.infoStr AndFontSize:16.0 AndWitdh:300.0];
    }
    //读取票面信息
    [self startLoadingViewWithText:@"正在读取门票信息"];
    [[WetlandService new] getTicketsList:^(NSArray *data) {
        self.ticketsList = data;
        self.checkedList = [NSMutableArray arrayWithCapacity:data.count];
        for (int i = 0, count = data.count; i < count ; i ++) {
            [self.checkedList addObject:[NSNumber numberWithBool:NO]];
        }
        [self.tableView   reloadData];
       
        [self stopLoadingView];
    } Failure:^(NSString *msg) {
        [self showAlertWithTitle:@"错误" Msg:msg];
        [self stopLoadingView];
    }];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table Method
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return (self.ticketsList?self.ticketsList.count:0);
    } else {
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return  CGRectGetHeight(_headerView.frame);
    } else {
        return 1.0;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return _headerView;
    } else {
        return nil;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 88.0;
    } else if (indexPath.section == 1) {
        return self.infoHeight;
    } else {
        return 44.0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        BookingTicketCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"BookingTicketCell" owner:self options:nil] objectAtIndex:0];
        NSDictionary *dic = [self.ticketsList objectAtIndex:indexPath.row];
        cell.infoLabel.text = [dic objectForKey:@"cname"];
//        cell.priceLabel.text = [dic objectForKey:@"fsellPrice"];
        cell.priceLabel.hidden = YES;
        cell.checkButton.tag = indexPath.row;
        cell.checkButton.userInteractionEnabled = NO;
        if ([[self.checkedList objectAtIndex:indexPath.row] boolValue]) {
            [cell.checkButton setImage:[UIImage imageNamed:@"checkbox"] forState:UIControlStateNormal];
        } else {
            [cell.checkButton setImage:[UIImage imageNamed:@"uncheckbox"] forState:UIControlStateNormal];
        }
        return cell;
    }
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"booking"];
    if (indexPath.section == 1) {
        CGRect frame = CGRectMake(0, 0, 320, self.infoHeight);
        UITextView *infoView = [[UITextView alloc] initWithFrame:frame];
        infoView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        infoView.text = self.infoStr;
        infoView.font = [UIFont systemFontOfSize:16.0];
        infoView.editable = NO;
        infoView.scrollEnabled = NO;
        infoView.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:infoView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.section == 2) {
        cell.textLabel.text = @"费用说明";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (indexPath.section == 3) {
        cell.textLabel.text = @"预定须知";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if ([[self.checkedList objectAtIndex:indexPath.row] boolValue]) {
            //取消选中
            [self.checkedList replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:NO]];
        } else {
            //选中
            [self.checkedList replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];
        }
        [self.tableView reloadData];
    }
    if (indexPath.section == 1) {
        //
    }
    if (indexPath.section == 2) {
        NSLog(@"点击了费用说明");
        BookingFeeIntroViewController *feeVC = [[BookingFeeIntroViewController alloc] initWithNibName:@"BookingFeeIntroViewController" bundle:nil];
        [self.navigationController pushViewController:feeVC animated:YES];
    }
    if (indexPath.section == 3) {
        NSLog(@"点击了预定须知");
        BookingNoticeViewController *noticeVC = [[BookingNoticeViewController alloc] initWithNibName:@"BookingNoticeViewController" bundle:nil];
        [self.navigationController pushViewController:noticeVC animated:YES];
    }

}

#pragma mark - Button Method
- (IBAction)bookingButtonAction:(id)sender
{
    NSLog(@"点击了立即预定");
    if (self.checkedList == nil || self.checkedList.count == 0) {
        [self showAlertWithTitle:@"错误" Msg:@"获取门票信息失败，请返回重试"];
        return;
    }
    //检查选中的票
    NSMutableArray *resultList = [NSMutableArray array];
    for (int i = 0, count = self.checkedList.count; i < count; i ++) {
        BOOL flag = [[self.checkedList objectAtIndex:i] boolValue];
        if (flag) {
            NSDictionary *dic = [self.ticketsList objectAtIndex:i];
            [resultList addObject:dic];
        }
    }
    if (resultList.count == 0) {
        [self showAlertWithTitle:@"错误" Msg:@"您至少需要选择一种票"];
        return;
    }
    WetLandBookingOrderViewController *orderVC = [[WetLandBookingOrderViewController alloc] initWithNibName:@"WetLandBookingOrderViewController" bundle:nil];
    orderVC.ticketsList = resultList;
    [self.navigationController pushViewController:orderVC animated:YES];
}
@end
